package com.eascs.admin.o2o.back.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.eascs.admin.common.dao.BaseO2oDao;
import com.eascs.admin.o2o.back.entity.O2oBackEntity;
import com.eascs.admin.o2o.back.vo.O2oBackPageVo;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.common.util.StringUtil;

@Repository
public class O2oBackDao extends BaseO2oDao{

	public List<O2oBackEntity> getEntityByLoanApplyNo(String loanapplyNo){
		String sql =  "select * from o2o_compensatory_back ocb where ocb.loan_apply_no = ? and ocb.flow_status in ('NEW','RUN')";
		return this.findEntitys(sql, O2oBackEntity.class, loanapplyNo);
	}
	
	public int getCountByLoanApplyNo(String loanapplyNo){
		String sql =  "select count(*) from o2o_compensatory_back ocb where ocb.loan_apply_no = ? and ocb.flow_status = 'RUN'";
		return this.findInteger(sql, loanapplyNo);
	}
	
	public PageResult<Object[]> getO2oBackEntitys(String loanapplyNo){
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, Integer.MAX_VALUE, 0);
		String sql =  "select ocb.id,ocb.loan_billcode,ocb.create_time,ocb.operate_name,ocb.loan_appeal from o2o_compensatory_back ocb where ocb.loan_apply_no = ? ";
		sqlPageQuery.addParam(loanapplyNo);
		sqlPageQuery.setQuerySql(sql);
		return this.findDataWithPage(sqlPageQuery);
	}

	public PageResult<Object[]> getByPage(O2oBackPageVo vo) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, vo.getPageSize(), vo.getPageIndex());
		StringBuffer querySql = new StringBuffer();
		querySql.append(" SELECT ocb.id,ocb.loan_apply_no,ocb.loan_billcode,ocb.loanner_name,ocb.loan_num,ocb.flow_status ");
		querySql.append(" FROM o2o_compensatory_back ocb ");
		querySql.append(" LEFT JOIN o2o_loanapply ol ON ocb.loan_apply_no=ol.loanapply_no ");
		querySql.append(" WHERE /**DATA_PERMISSION(ol.ltd)**/ 1=1 ");
		if(!StringUtil.isEmpty(vo.getLoan_billcode())){
			querySql.append(" and ocb.loan_billcode like ?");
			sqlPageQuery.addLikeParam(vo.getLoan_billcode());
		}
		if(!StringUtil.isEmpty(vo.getLoanner_name())){
			querySql.append(" and ocb.loanner_name like ?");
			sqlPageQuery.addLikeParam(vo.getLoanner_name());
		}
		if(!StringUtil.isEmpty(vo.getLoan_num())){
			querySql.append(" and ocb.loan_num = ?");
			sqlPageQuery.addParam(vo.getLoan_num());
		}
		if(!StringUtil.isEmpty(vo.getFlow_status())){
			querySql.append(" and ocb.flow_status = ? ");
			sqlPageQuery.addParam(vo.getFlow_status());
		}
		querySql.append("  ORDER BY ocb.create_time DESC ");
		sqlPageQuery.setQuerySql(querySql.toString());
		return this.findDataWithPage(sqlPageQuery);
	}

	public int getCountPlan(String mainId) {
		String sql =  "select count(*) from o2o_ea_loan_data_td ocb where ocb.main_id = ? and ocb.is_del = 'N'";
		return this.findInteger(sql, mainId);
	}

}
